package cn.gp.service.impl;

import cn.gp.common.PageInfo;
import cn.gp.entity.Category;
import cn.gp.mapper.CategoryMapper;
import cn.gp.service.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @ClassName CategoryServiceImpl
 * @Description TODO
 * @Author He_Guoping
 * @Date 2023/5/27 15:42
 * @Version 1.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageInfo<Category> page(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        Page<Category> p = (Page<Category>) categoryMapper.list(name);
        return new PageInfo<>(p.getTotal(), p.getResult());
    }

    @Override
    public Boolean add(Category category) {
        categoryMapper.save(category);
        return true;
    }

    @Override
    public Boolean update(Category category) {
        categoryMapper.update(category);
        return true;
    }

    @Override
    public Boolean delete(Integer id) {
        categoryMapper.deleteById(id);
        return true;
    }

    @Override
    public List<Category> getCategoryList() {
        return categoryMapper.getCategoryList();
    }


//    @Override
//    public PageInfo<Category> page(Integer page, Integer pageSize) {
//        Long count = categoryMapper.count();
//        List<Category> list = categoryMapper.page((int) ((page - 1) * count), pageSize);
//        return new PageInfo<>(count, list);
//    }



//    @Override
//    public PageInfo<Category> page(Integer page, Integer pageSize) {
//        com.github.pagehelper.PageInfo<Object> objectPageInfo = PageHelper.startPage(page, pageSize).doSelectPageInfo(() -> categoryMapper.list());
//        log.info("pageInfo, 看看我查询出来的结果是什么：\n");
//        Arrays.stream(objectPageInfo.getNavigatepageNums()).forEach(System.out::println);
//        log.info("看看我查询出来的结果是什么：" + objectPageInfo.getPages());
//        System.out.println("------");
//        Page<Object> objects = PageHelper.startPage(page, pageSize).doSelectPage(() -> categoryMapper.list());
//        System.out.println("page, " + objects.toString());
//        System.out.println(objects.getTotal());
//        return null;
//    }

}
